package com.department.mydepartment.controller;

import com.department.mydepartment.repository.UserRepository;
import com.department.mydepartment.service.UserService;
import com.department.mydepartment.util.ShowData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class UserController {

    @Autowired
    UserRepository userRepository;

    @Autowired
    UserService userService;

    @Autowired
    ShowData showData;

    //登录
    @PostMapping(path = "/login")
    public String login(@RequestParam("userId") String userId, @RequestParam("password") String password) {
        if ("".equals(userId) || userId == null || "".equals(password) || password == null) {
            return showData.showData(false, "请输入用户名和密码！");
        }
        return userService.login(userId, password);
    }

    //查找
    @GetMapping(path = "/findAllUser")
    public String findAllUser() {
        return userService.findAllUser();
    }

    @GetMapping(path = "/findByUserId")
    public String findByUserId(@RequestParam("userId") String userId) {
        if ("".equals(userId) || userId == null) {
            return showData.showData(false, "用户不存在");
        }
        return userService.findByUserId(userId);
    }

    //编辑
    @PostMapping(path = "/editByUserId")
    public String editByUserId(@RequestParam("userId") String userId, @RequestParam("passwordUpdated")String passwordUpdated,
                               @RequestParam("passwordUpdated")String passwordConfirmed) {
        if ("".equals(userId) || userId == null) {
            return showData.showData(false, "用户不存在");
        }
        if (passwordUpdated != null && passwordConfirmed != null) {
            if (passwordUpdated.equals(passwordConfirmed)) {
                return userService.editByUserId(userId, passwordConfirmed);
            }
            return showData.showData(false, "两次密码不一致");
        }
        return showData.showData(false, "请输入密码");
    }

    //删除
    @DeleteMapping(path = "/deleteByUserId")
    public String deleteByUserId(@RequestParam("userId") String userId) {
        if ("".equals(userId) || userId == null) {
            return showData.showData(false, "用户不存在");
        }
        return userService.deleteByUserId(userId);
    }

    @DeleteMapping(path = "/deleteUserList")
    public String deleteArticleList(@RequestParam("userIdList") String userIdList) {
        if ("".equals(userIdList) || userIdList == null) {
            return showData.showData(false, "用户不存在");
        }
        return userService.deleteByUserId(userIdList);
    }
}
